<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

class CustomizePayWay extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'customize_pay_way';

    const STATUS_NORMAL = 1;
    const STATUS_CLOSED = 0;

    const RECORD_STATUS_DESC = [
        self::STATUS_NORMAL => '正常',
        self::STATUS_CLOSED => '失效',
    ];

    const PAY_TYPE_SH_POS = 1;
    const PAY_TYPE_SH_TRANSFER = 2;
    const PAY_TYPE_SH_CASH = 3;
    const PAY_TYPE_BJ_POS_ALIPAY = 4;
    const PAY_TYPE_BJ_POS_WECHAT = 5;
    const PAY_TYPE_SH_TRANSFER_ALIPAY = 6;
    const PAY_TYPE_BJ_POS_BANKCARD = 7;
    const PAY_TYPE_BJ_TRANSFER_ALIPAY = 8;
    const PAY_TYPE_BJ_CASH = 9;
    const PAY_TYPE_BJ_TRANSFER = 10;
    const PAY_TYPE_SH_WECHAT = 11;
    const PAY_TYPE_BJ_WECHAT = 12;
    const PAY_TYPE_SH_TRANSFER_ALIPAY_TP = 13;
    const PAY_TYPE_DESC = [
        self::PAY_TYPE_SH_POS                => '上海POS机',
        self::PAY_TYPE_SH_TRANSFER           => '上海转账',
        self::PAY_TYPE_SH_TRANSFER_ALIPAY    => '上海支付宝转账（家宅一生）',
        self::PAY_TYPE_SH_TRANSFER_ALIPAY_TP => '上海支付宝转账（节选家什）',
        self::PAY_TYPE_SH_CASH               => '上海现金',
        self::PAY_TYPE_SH_WECHAT             => '上海微信转账',
        self::PAY_TYPE_BJ_POS_ALIPAY         => '北京POS机-支付宝',
        self::PAY_TYPE_BJ_POS_WECHAT         => '北京POS机-微信',
        self::PAY_TYPE_BJ_POS_BANKCARD       => '北京POS机-银行卡',
        self::PAY_TYPE_BJ_TRANSFER_ALIPAY    => '北京支付宝转账（节选家什）',
        self::PAY_TYPE_BJ_TRANSFER           => '北京转账',
        self::PAY_TYPE_BJ_CASH               => '北京现金',
        self::PAY_TYPE_BJ_WECHAT             => '北京微信转账'
    ];
    const PAY_TYPE_BJ_DESC = [
        self::PAY_TYPE_BJ_POS_ALIPAY      => '北京POS机-支付宝',
        self::PAY_TYPE_BJ_POS_WECHAT      => '北京POS机-微信',
        self::PAY_TYPE_BJ_POS_BANKCARD    => '北京POS机-银行卡',
        self::PAY_TYPE_BJ_TRANSFER_ALIPAY => '北京支付宝转账（节选家什）',
        self::PAY_TYPE_BJ_TRANSFER        => '北京转账',
        self::PAY_TYPE_BJ_CASH            => '北京现金',
        self::PAY_TYPE_BJ_WECHAT          => '北京微信转账'
    ];
    const PAY_TYPE_SH_DESC = [
        self::PAY_TYPE_SH_POS                => '上海POS机',
        self::PAY_TYPE_SH_TRANSFER           => '上海转账',
        self::PAY_TYPE_SH_TRANSFER_ALIPAY    => '上海支付宝转账（家宅一生）',
        self::PAY_TYPE_SH_TRANSFER_ALIPAY_TP => '上海支付宝转账（节选家什）',
        self::PAY_TYPE_SH_CASH               => '上海现金',
        self::PAY_TYPE_SH_WECHAT             => '上海微信转账'
    ];

    const RECORD_TYPE_PAY = 1;
    const RECORD_TYPE_REFUND = 2;

    const RECORD_TYPE_DESC = [
        self::RECORD_TYPE_PAY    => '支付',
        self::RECORD_TYPE_REFUND => '退款'
    ];

    public static function getRecordList($where)
    {
        $where['AND']['status'] = self::STATUS_NORMAL;
        $where['ORDER'] = ['id' => 'DESC'];
        $list = self::select("*", $where);
        $total = self::count('*', $where['AND']);
        return compact('list', 'total');
    }

    public static function getRecordAdminList()
    {
        return self::select('admin_id', [
            'AND'   => ['status' => self::STATUS_NORMAL],
            'GROUP' => ['admin_id']
        ]);
    }

    public static function addPayRecord($orderNo, $orderAmount, $payType, $payNo, $payAmount, $adminId, $remark)
    {
        self::insert([
            'order_no'     => $orderNo,
            'order_amount' => $orderAmount,
            'pay_type'     => $payType,
            'pay_no'       => $payNo,
            'pay_amount'   => $payAmount,
            'remark'       => $remark,
            'type'         => self::RECORD_TYPE_PAY,
            'status'       => self::STATUS_NORMAL,
            'add_time'     => date('Y-m-d H:i:s'),
            'admin_id'     => $adminId
        ]);
    }

    public static function addRefundRecord($refundOrderNo, $orderAmount, $payType, $payNo, $payAmount, $adminId, $remark)
    {
        self::insert([
            'order_no'     => $refundOrderNo,
            'order_amount' => $orderAmount,
            'pay_type'     => $payType,
            'pay_no'       => $payNo,
            'pay_amount'   => $payAmount,
            'remark'       => $remark,
            'type'         => self::RECORD_TYPE_REFUND,
            'status'       => self::STATUS_NORMAL,
            'add_time'     => date('Y-m-d H:i:s'),
            'admin_id'     => $adminId
        ]);
    }


    public static function getPayRecord($orderNo)
    {
        return self::get("*", [
                'order_no' => $orderNo,
                'type'     => self::RECORD_TYPE_PAY,
                'status'   => self::STATUS_NORMAL,
            ]
        );
    }

    public static function hasRefundRecord($refundOrderNo)
    {
        return self::has([
                'order_no' => $refundOrderNo,
                'type'     => self::RECORD_TYPE_REFUND,
                'status'   => self::STATUS_NORMAL,
            ]
        );
    }

    public static function getRefundRecord($refundOrderNo, $refundAmount)
    {
        return self::get("*", [
                'order_no'     => $refundOrderNo,
                'order_amount' => $refundAmount,
                'type'         => self::RECORD_TYPE_REFUND,
                'status'       => self::STATUS_NORMAL,
            ]
        );
    }

    public static function getRecordByRecordId($recordId)
    {
        return self::get("*", [
                'id'     => $recordId,
                'status' => self::STATUS_NORMAL
            ]
        );
    }

    public static function updateRecord($recordId, $data)
    {
        self::update($data, [
            'id'     => $recordId,
            'status' => self::STATUS_NORMAL
        ]);
    }

}